* Data
use "$data/ctb_maindata.dta", clear

* Keep respondents with no variation in chosen option
* for imputation
* drop if choice_sd==.

* Explore missingness
mdesc a_ind_ctb b_ind_ctb d_ind_ctb
su d_ind_ctb, det

* Risk aversion: a_inc_ctb
* Greater than 1: Risk-acceptant
* Less than 1: Risk-averse
gen riskacceptance_ctb=a_ind_ctb
lab var riskacceptance_ctb "Risk acceptance CTB"

* Winsorize at 5th and 95th percentile - within treatment group and for the pooled sample
* Make an indicator for outlier observations 
g riskacceptance_ctb_win=.
g riskacceptance_ctb_outlier=0
g riskacceptance_ctb_outlierp5=0
g riskacceptance_ctb_outlierp95=0
forval i=1/4 {
su riskacceptance_ctb if ctb==`i', det
replace riskacceptance_ctb_win=riskacceptance_ctb if ctb==`i'
replace riskacceptance_ctb_win=`r(p95)' if riskacceptance_ctb>=`r(p95)' & ctb==`i' & riskacceptance_ctb!=.
replace riskacceptance_ctb_win=`r(p5)' if riskacceptance_ctb<=`r(p5)' & ctb==`i' & riskacceptance_ctb!=. 
replace riskacceptance_ctb_outlier=1 if (riskacceptance_ctb>=`r(p95)' | riskacceptance_ctb<=`r(p5)') & ctb==`i' & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp5=1 if riskacceptance_ctb<=`r(p5)' & ctb==`i' & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp95=1 if riskacceptance_ctb>=`r(p95)' & ctb==`i' & riskacceptance_ctb!=.
}
lab var riskacceptance_ctb_win "Risk acceptance CTB winsorized" 
lab var riskacceptance_ctb_outlier "Risk acceptance CTB 10 % outlier"
lab var riskacceptance_ctb_outlierp5 "Risk acceptance CTB 5 % p5 outlier"
lab var riskacceptance_ctb_outlierp95 "Risk acceptance CTB 5 % p95 outlier"

su riskacceptance_ctb, det
g riskacceptance_ctb_win_pooled=riskacceptance_ctb
g riskacceptance_ctb_outlier_p=0
g riskacceptance_ctb_outlierp5_p=0
g riskacceptance_ctb_outlierp95_p=0
replace riskacceptance_ctb_win_pooled=`r(p95)' if riskacceptance_ctb>=`r(p95)' & riskacceptance_ctb!=.
replace riskacceptance_ctb_win_pooled=`r(p5)' if riskacceptance_ctb<=`r(p5)' & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlier_p=1 if (riskacceptance_ctb>=`r(p95)' | riskacceptance_ctb<=`r(p5)') & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp5_p=1 if riskacceptance_ctb<=`r(p5)' & riskacceptance_ctb!=.
replace riskacceptance_ctb_outlierp95_p=1 if riskacceptance_ctb>=`r(p95)' & riskacceptance_ctb!=.
lab var riskacceptance_ctb_win_pooled "Risk acceptance CTB winsorized pooled"
lab var riskacceptance_ctb_outlier_p "Risk acceptance CTB 10 % outlier pooled"
lab var riskacceptance_ctb_outlierp5_p "Risk acceptance CTB 5 % p5 outlier pooled"
lab var riskacceptance_ctb_outlierp95_p "Risk acceptance CTB 5 % p95 outlier pooled"

* Generate imputed risk acceptance measure
* Always latest set to 95th percentile
* Always soonest set to 5th percentile
gen riskacceptance_ctb_imp=riskacceptance_ctb_win_pooled
su riskacceptance_ctb, det
replace riskacceptance_ctb_imp=`r(p5)' if cbt_alw_soonest==1
replace riskacceptance_ctb_imp=`r(p95)' if cbt_alw_latest==1

* Trim at 5th and 95th percentile - within treatment group and for the pooled sample
g riskacceptance_ctb_trim=.
forval i=1/4 {
su riskacceptance_ctb if ctb==`i', det
replace riskacceptance_ctb_trim=riskacceptance_ctb if ctb==`i'
replace riskacceptance_ctb_trim=. if riskacceptance_ctb>`r(p95)' & ctb==`i'
replace riskacceptance_ctb_trim=. if riskacceptance_ctb<`r(p5)' & ctb==`i'
}
lab var riskacceptance_ctb_trim "Risk acceptance CTB trimmed"

su riskacceptance_ctb, det
g riskacceptance_ctb_trim_pooled=riskacceptance_ctb
replace riskacceptance_ctb_trim_pooled=. if riskacceptance_ctb>`r(p95)'
replace riskacceptance_ctb_trim_pooled=. if riskacceptance_ctb<`r(p5)' 
lab var riskacceptance_ctb_trim_pooled "Risk acceptance CTB trimmed pooled"

* Indicator
gen riskacceptance_ctb_win_high=.
su riskacceptance_ctb_win, det
replace riskacceptance_ctb_win_high=0 if riskacceptance_ctb_win<`r(p50)'
replace riskacceptance_ctb_win_high=1 if riskacceptance_ctb_win>=`r(p50)'

gen riskacceptance_ctb_win_norm=riskacceptance_ctb_win
su riskacceptance_ctb_win_norm, det
replace riskacceptance_ctb_win_norm=(riskacceptance_ctb_win-`r(mean)')/`r(sd)'


* Time preferences
gen patience_ctb=d_ind_ctb
lab var patience_ctb "Patience CTB"


* Implied annual discount rate
gen drate_ind = patience_ctb^(-365) - 1
*gen drate_ind_alt= patience_ctb^(1/365) - 1
label var drate_ind "Annualized Discount Rate"

* Winsorize at 5th and 95th percentile - within treatment group and for the pooled sample
* Make an indicator for outlier observations 
g patience_ctb_win=.
g patience_ctb_outlier=0
g patience_ctb_outlierp5=0
g patience_ctb_outlierp95=0
forval i=1/4 {
su patience_ctb if ctb==`i', det
replace patience_ctb_win=patience_ctb if ctb==`i'
replace patience_ctb_win=`r(p95)' if patience_ctb>`r(p95)' & ctb==`i' & patience_ctb!=.
replace patience_ctb_win=`r(p5)' if patience_ctb<`r(p5)' & ctb==`i' & patience_ctb!=. 
replace patience_ctb_outlier=1 if (patience_ctb>`r(p95)' | patience_ctb<`r(p5)') & ctb==`i' & patience_ctb!=.
replace patience_ctb_outlierp5=1 if patience_ctb<`r(p5)' & ctb==`i' & patience_ctb!=.
replace patience_ctb_outlierp95=1 if patience_ctb>`r(p95)' & ctb==`i' & patience_ctb!=.
}
lab var patience_ctb_win "Patience CTB winsorized" 
lab var patience_ctb_outlier "Patience CTB 10 % outlier"
lab var patience_ctb_outlierp5 "Patience CTB 5 % p5 outlier"
lab var patience_ctb_outlierp95 "Patience CTB 5 % p95 outlier"


gen drate_ind_win = patience_ctb_win^(-365) - 1
label var drate_ind_win "Annualized Discount Rate (Winsorized)"

gen drate_ind_test = patience_ctb^(-365) - 1


su patience_ctb, det
g patience_ctb_win_pooled=patience_ctb
g patience_ctb_outlier_p=0
g patience_ctb_outlierp5_p=0
g patience_ctb_outlierp95_p=0
replace patience_ctb_win_pooled=`r(p95)' if patience_ctb>`r(p95)' & patience_ctb!=.
replace patience_ctb_win_pooled=`r(p5)' if patience_ctb<`r(p5)' & patience_ctb!=.
replace patience_ctb_outlier_p=1 if (patience_ctb>`r(p95)' | patience_ctb<`r(p5)') & patience_ctb!=.
replace patience_ctb_outlierp5_p=1 if patience_ctb<`r(p5)' & patience_ctb!=.
replace patience_ctb_outlierp95_p=1 if patience_ctb>`r(p95)' & patience_ctb!=.
lab var patience_ctb_win_pooled "Patience CTB winsorized pooled"
lab var patience_ctb_outlier_p "Patience CTB 10 % outlier pooled"
lab var patience_ctb_outlierp5_p "Patience CTB 5 % p5 outlier pooled"
lab var patience_ctb_outlierp95_p "Patience CTB 5 % p95 outlier pooled"

gen drate_ind_win_pooled = patience_ctb_win_pooled^(-365) - 1
label var drate_ind_win_pooled "Annualized Discount Rate (Winsorized Pooled)"

* Generate imputed measure
* Always latest set to 95th percentile
* Always soonest set to 5th percentile
su patience_ctb_win_pooled, det
*gen patience_ctb_imp=patience_ctb_win_pooled
gen patience_ctb_imp=patience_ctb
replace patience_ctb_imp=`r(min)' if cbt_alw_soonest==1
replace patience_ctb_imp=`r(max)' if cbt_alw_latest==1
pwcorr patience_ctb_imp patience_ctb, sig

gen drate_ind_imp = patience_ctb_imp^(-365) - 1
label var drate_ind_imp "Annualized Discount Rate (Imputed)"
su drate_ind_imp, det

* Indicator
gen patience_ctb_imp_high=.
su patience_ctb_imp, det
replace patience_ctb_imp_high=0 if patience_ctb_imp<`r(p50)'
replace patience_ctb_imp_high=1 if patience_ctb_imp>=`r(p50)'
pwcorr patience_ctb_imp patience_ctb_imp_high, sig

* Generate alternative imputed measure
su patience_ctb_win, det
gen patience_ctb_imp_alt=.
replace patience_ctb_imp_alt=patience_ctb_win

* Alternative imputed measure
* Distinguish between speeders and non-speeders
* based on median interview length
* code those who were in the top 25% on LOI as speeders

gen notswitch=.
replace notswitch=0 if choice_sd!=0
replace notswitch=1 if choice_sd==0
bysort ctb: tab notswitch
tab notswitch

* Among those who always selected earliest payment
* code those as impatient who took more time to complete the 
* survey
gen nonspeeder=0
su length if cbt_alw_soonest==1, det
replace nonspeeder=1 if cbt_alw_soonest==1 & length>=`r(p50)' & length<.
su patience_ctb_imp_alt, det
replace patience_ctb_imp_alt=`r(min)' if nonspeeder==1 & cbt_alw_soonest==1

* Among those who always selected latest payment
* code those as patient who took more time to complete the 
* survey 
su length if cbt_alw_latest==1, det
replace nonspeeder=1 if cbt_alw_latest==1 & length>=`r(p50)' & length<.
su patience_ctb_imp_alt, det
replace patience_ctb_imp_alt=`r(max)' if nonspeeder==1 & cbt_alw_latest==1

* Generate centered winsorized CTB measure: subtract mean
* and divide by sd
gen patience_ctb_win_norm=patience_ctb_win
su patience_ctb_win_norm, det
replace patience_ctb_win_norm=(patience_ctb_win-`r(mean)')/`r(sd)'
 

* Impute centered and winsorized CTB measure
su patience_ctb_win_norm, det
gen patience_ctb_win_norm_imp=patience_ctb_win_norm
replace patience_ctb_win_norm_imp=`r(min)' if nonspeeder==1 & cbt_alw_soonest==1
replace patience_ctb_win_norm_imp=`r(max)' if nonspeeder==1 & cbt_alw_latest==1

* Trim at 5th and 95th percentile - within treatment group and for the pooled sample
g patience_ctb_trim=.
forval i=1/4 {
su patience_ctb if ctb==`i', det
replace patience_ctb_trim=patience_ctb if ctb==`i'
replace patience_ctb_trim=. if patience_ctb>`r(p95)' & ctb==`i'
replace patience_ctb_trim=. if patience_ctb<`r(p5)' & ctb==`i'
}
lab var patience_ctb_trim "Patience CTB trimmed"

* Indicator
su patience_ctb_trim, det
gen patience_ctb_trim_high=.
replace patience_ctb_trim_high=0 if patience_ctb_trim<`r(p50)'
replace patience_ctb_trim_high=1 if patience_ctb_trim>=`r(p50)' & patience_ctb_trim<.
label var patience_ctb_trim_high "Patience CTB: High"

gen drate_ind_trim = patience_ctb_trim^(-365) - 1
label var drate_ind_trim "Annualized Discount Rate"
su drate_ind_trim, det

su patience_ctb, det
g patience_ctb_trim_pooled=patience_ctb
replace patience_ctb_trim_pooled=. if patience_ctb>`r(p95)'
replace patience_ctb_trim_pooled=. if patience_ctb<`r(p5)'
lab var patience_ctb_trim_pooled "Patience CTB trimmed pooled"

gen drate_ind_trim_pooled = patience_ctb_trim_pooled^(-365) - 1
label var drate_ind_trim_pooled "Annualized Discount Rate (Pooled)"
su drate_ind_trim_pooled, det

pwcorr length cbt_alw_soonest cbt_alw_latest, sig

* Patience CTB high low indicator
su patience_ctb, det
gen patience_ctb_high=.
replace patience_ctb_high=0 if patience_ctb<`r(p50)'
replace patience_ctb_high=1 if patience_ctb>=`r(p50)' & patience_ctb<.

su drate_ind, det
gen drate_ind_high=.
replace drate_ind_high=0 if drate_ind<`r(p50)'
replace drate_ind_high=1 if drate_ind>=`r(p50)' & drate_ind<.
label var drate_ind_high "Annualized Discount Rate: High"

* Times 100
foreach var in ///
patience_ctb patience_ctb_win ///
patience_ctb_trim patience_ctb_imp patience_ctb_high ///
riskacceptance_ctb riskacceptance_ctb_trim riskacceptance_ctb_win ///
drate_ind drate_ind_trim drate_ind_win drate_ind_high {
gen `var'100=`var'*100
gen `var'log=log(`var')
gen `var'log100=(log(`var'))*100
}


* Save main data for analysis
save "$data/main_touse.dta", replace
